package com.tools.cleanmaster.functionpage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.tools.cleanmaster.utils.UiUtils;

public class SchematicLineView extends View {
    private int mScreenHeight;
    private int mScreenWidth;
    private boolean isBeeLine = true; // 标识当前是否是直线，如果否的话表示线段
    private int spaceLine = 5; // 线段之前的空隙大小
    private int countLine = 5; // 线段条数
    private int threshold = 50; // 阈值，判断当前是高低状态
    private int currentValue = 70; // 当前值
    private int startLowColor;
    private int endLowColor;
    private int startMiddleColor;
    private int endMiddleColor;
    private int startHeightColor;
    private int endHeightColor;
    private int commonHeightColor;
    private int commonLowColor;
    private int defaultColor;
    private int defaultHeightColor;
    private int defaultLowColor;
    private int lineWidth = 5; // 标识线段高度
    private Paint linePaint;
    private Paint defaultPaint; // 默认线段画笔
    LinearGradient linearGradient;


    public SchematicLineView(Context context) {
        super(context);
    }

    public SchematicLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SchematicLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        defaultPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        defaultPaint.setStyle(Paint.Style.FILL);
        lineWidth = UiUtils.dipToPx(context, 2);
        spaceLine = UiUtils.dipToPx(context, 2);
        startHeightColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.start_heigth_color);
        startLowColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.start_low_color);
        endHeightColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.end_heigth_color);
        endLowColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.end_low_color);
        startMiddleColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.start_middle_color);
        endMiddleColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.end_middle_color);
        commonHeightColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.common_height_color);
        commonLowColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.common_low_color);
        defaultColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.default_common_color);
        defaultHeightColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.default_height_color);
        defaultLowColor = ContextCompat.getColor(context, com.tools.cleanmaster.R.color.default_low_color);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mScreenHeight = h;
        mScreenWidth = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        linePaint.setStrokeWidth(lineWidth);
        defaultPaint.setStrokeWidth(lineWidth);
        if (isBeeLine) {
            defaultPaint.setColor(defaultColor);
            onDrawBeeLine(canvas);
        } else {
            onDrawSegmentLine(canvas);
        }
    }

    void onDrawBeeLine(Canvas canvas) {
        if (currentValue > threshold) {
            linearGradient = new LinearGradient(0, 0, mScreenWidth * currentValue / 100, 0, startHeightColor, endHeightColor, Shader.TileMode.CLAMP);
        } else if (currentValue <= 10) {
            linearGradient = new LinearGradient(0, 0, mScreenWidth * currentValue / 100, 0, startLowColor, endLowColor, Shader.TileMode.CLAMP);
        } else {
            linearGradient = new LinearGradient(0, 0, mScreenWidth * currentValue / 100, 0,
                    startMiddleColor, endMiddleColor, Shader.TileMode.CLAMP);
        }
        linePaint.setShader(linearGradient);
        canvas.drawLine(0, mScreenHeight / 2, mScreenWidth * currentValue / 100, mScreenHeight / 2, linePaint);
        canvas.drawLine(mScreenWidth * currentValue / 100, mScreenHeight / 2, mScreenWidth, mScreenHeight / 2, defaultPaint);
    }

    void onDrawSegmentLine(Canvas canvas) {
        linePaint.setShader(null);
        linePaint.setColor(currentValue > threshold ? commonHeightColor : commonLowColor);
        defaultPaint.setColor(currentValue > threshold ? defaultHeightColor : defaultLowColor);
        float width = (mScreenWidth - (countLine - 1) * spaceLine) / countLine;
        for (int i = 0; i < countLine; i++) {
            if (currentValue * countLine > i * 100) {
                canvas.drawLine((width + spaceLine) * i, mScreenHeight / 2, (width + spaceLine) * i + width, mScreenHeight / 2, linePaint);
            } else {
                canvas.drawLine((width + spaceLine) * i, mScreenHeight / 2, (width + spaceLine) * i + width, mScreenHeight / 2, defaultPaint);
            }
        }
    }

    public void setIsBeeLine(Boolean isBeeLine) {
        this.isBeeLine = isBeeLine;
        invalidate();
    }

    public void setCurrentValue(int value) {
        this.currentValue = value;
        invalidate();
    }

    public void setThreshold(int threshold) {
        this.threshold = threshold;
        invalidate();
    }
}

